package com.njxzc.igo.Dao;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.njxzc.igo.model.Address;
import com.njxzc.igo.model.City;
import com.njxzc.igo.model.Province;
import com.njxzc.igo.model.User;
import com.njxzc.igo.utils.HibernateUtils;

public class AddressDaoImpl implements AddressDao{

	public Address addAddressByUser(User user, String receiverName, String phone, String province, String city,
			String region, String detailsAddress) {
		Session session = null;
		Transaction tx = null;
		Address address = new Address();
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			session.update(user);
			address.setReceiverName(receiverName);
			address.setPhone(phone);
			address.setProvince(province);
			address.setCity(city);
			address.setRegion(region);
			address.setDetailsAddress(detailsAddress);
			address.setIsDefault(false);
			address.setUser(user);
			session.save(address);
			session.flush();
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
		}
		return address;
		
	}

	public Set findAllByUser(String username) {
		Session session = null;
		Transaction tx = null;
		List list = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			list =session.createQuery("from Address where user.username=?").setString(0, username).list();
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
		}
		return new HashSet(list);
	}

	@Override
	public Address findById(int addressId) {
		Session session = null;
		Transaction tx = null;
		Address address  =null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			address = (Address) session.get(Address.class, addressId);
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
		}
		return address;
	}

	@Override
	public City findByCityId(String cityId) {
		Session session = null;
		Transaction tx = null;
		City city =null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			city = (City) session.get(City.class, cityId);
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
		}
		return city;

	}

	@Override
	public List<Province> findAllProvinces() {
		List<Province> provinces=null;
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			provinces = session.createQuery("from Province").list();
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
		}
		return provinces;
	}

	@Override
	public List<City> findAllByProvincesId(String provinceId) {
		List<City> citys=null;
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			citys = session.createQuery("from City as city where city.provice = ?").setString(0, provinceId).list();
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
		}
		return citys;
		
	}
	
}
